package com.lowkey.sqlTools;

import com.lowkey.bean.Book;
import com.lowkey.dataSource.DBControl;
import com.lowkey.tableInfo.TableInfo;
import com.lowkey.tools.Constant;

public class LowkeyDetachedCriteria {
	private String sql;
	
	private TableInfo table;
	//首先要拼接sql语句的前半部分,select * from book
	public void setEntity(Class c){
		TableInfo table = Constant.TABLEMAP.get(c.getName());
		setTable(table);
		String sql = "select * from " + table.getTableName();
		setSql(sql);
	}
	static {
		load();
	}
	public static void load(){
		if(null==Constant.TABLEMAP){
			new DBControl();
		}
	}
	public void addRestrictions(Restrictions r){
		String sql =getSql();
		if(!sql.contains("where")){
			sql+=" where ";
		}
		sql+=r.getCondition();
		setSql(sql);
	}
	public static void main(String[] args) {
		LowkeyDetachedCriteria deta = new LowkeyDetachedCriteria();
		deta.setEntity(Book.class);
		deta.addRestrictions(new Restrictions().in("BookId", new Integer[]{1,2,3}));
		System.out.println(deta.getSql());
	}
	
	public TableInfo getTable() {
		return table;
	}
	public void setTable(TableInfo table) {
		this.table = table;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
}
